package com.mail.transfer.email;

import javax.mail.*;
import javax.mail.internet.MimeMessage;
import java.io.*;
import java.net.Socket;
import java.util.function.IntFunction;

/**
 * 用于获取邮箱中邮件信息的线程
 * @author xiaocg
 *
 */
public class RecvMailsThread extends Thread {
    private Message[] messages;
    private String host;

    public RecvMailsThread(Message[] messages,String host) {
        this.messages = messages;
        this.host = host;
    }

    private final static Integer TRANSFER_SIZE=4*1024*1024;

    @Override
    public void run() {
        if (null != messages) {
            for (int i = 0; i < messages.length; i++) {
                try {
                    MailInfo re = new MailInfo((MimeMessage) messages[i]);
                    re.setAttachPath("D:\\mailFiles");

                    System.out.println("邮件　" + i + "　主题:　" + re.getSubject());
                    boolean containAttach = re.isContainAttach((Part) messages[i]);
                    System.out.println("邮件　" + i + "　是否包含附件:　" + containAttach);
                    if (containAttach){
                        re.saveAttachMent((Part) messages[i]);

                    }
                    System.out.println("邮件　" + i + "　发送时间:　" + re.getSentDate());
                    System.out.println("邮件　" + i + "　发送人地址:　" + re.getFrom());
                    System.out.println("邮件　" + i + "　收信人地址:　" + re.getMailAddress("to"));
                    System.out.println("邮件　" + i + "　发送时间:　" + re.getSentDate());
                    re.getMailContent((Part) messages[i]);
                    System.out.println("邮件　" + i + "　正文内容:　\r\n" + re.getBodyText());

                    //建立socket 连接发送邮件信息

                    String ip = host.split(":")[0];
                    Integer port = Integer.parseInt(host.split(":")[1]);
                    Socket socket = new Socket(ip,port);
                    OutputStream bos = socket.getOutputStream();
                    BufferedInputStream input = new BufferedInputStream(new FileInputStream(re.getFiles().get(0)));
                    int available = input.available();
                    byte[] buf = new byte[TRANSFER_SIZE];
                    int remainder = available%TRANSFER_SIZE;
                    int merchant = available/TRANSFER_SIZE;
                    if (remainder!=0){
                        merchant = merchant + 1 ;
                    }
                    for (int ij=0;i<merchant;ij++){

                        input.read(buf);
                        if (merchant == 1){
                            SendDto dto = new SendDto(4, buf, "完成");
                            byte[] sendBytes = dto.toByteArray();
                            bos.write(sendBytes,0,sendBytes.length);
                            continue;
                        }

                        if (ij == 0 ){
                            SendDto dto = new SendDto(1, buf, "頭包");
                            byte[] sendBytes = dto.toByteArray();
                            bos.write(sendBytes,0,sendBytes.length);
                            continue;
                        }

                        if (ij == merchant-1){
                            SendDto dto = new SendDto(3, buf, "尾包");
                            byte[] sendBytes = dto.toByteArray();
                            bos.write(sendBytes,0,sendBytes.length);
                            continue;
                        }

                        SendDto dto = new SendDto(2, buf, "中包");
                        byte[] sendBytes = dto.toByteArray();
                        bos.write(sendBytes,0,sendBytes.length);
                    }

                    bos.close();
                    input.close();
                    socket.close();
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

